/*     */ package com.wzh.service.bookinfo;
/*     */ 
/*     */ import com.wzh.bean.book.BookChapter;
/*     */ import com.wzh.bean.book.BookInfo;
/*     */ import com.wzh.bean.book.BookType;
/*     */ import com.wzh.constant.CommonConstant;
/*     */ import com.wzh.service.base.DaoSupport;
/*     */ import com.wzh.service.bookinfo.booktype.BookTypeService;
/*     */ import com.wzh.service.bookinfo.booktype.BookTypeServiceBean;
/*     */ import com.wzh.service.bookinfo.chapter.BookChapterService;
/*     */ import com.wzh.service.bookinfo.chapter.BookChapterServiceBean;
/*     */ import com.wzh.utils.CommonUtil;
/*     */ import com.wzh.utils.PropertiesUtil;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import javax.persistence.EntityManager;
/*     */ import javax.persistence.Query;
/*     */ import javax.servlet.ServletContext;
/*     */ import org.apache.struts2.ServletActionContext;
/*     */ import org.apache.tools.zip.ZipEntry;
/*     */ import org.apache.tools.zip.ZipOutputStream;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Propagation;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ @Transactional
/*     */ public class BookInfoServiceBean extends DaoSupport<BookInfo>
/*     */   implements BookInfoService
/*     */ {
/*     */   public void save(BookInfo book)
/*     */   {
/*  40 */     if (book == null) return;
/*     */ 
/*  42 */     Query query = this.em.createQuery("select o from BookInfo o where o.title=?1");
/*  43 */     query.setParameter(1, book.getTitle());
/*  44 */     List list = query.getResultList();
/*  45 */     if ((list != null) && (list.size() > 0))
/*  46 */       book.setId(((BookInfo)list.get(0)).getId());
/*     */     else
/*  48 */       super.save(book);
/*     */   }
/*     */ 
/*     */   public void delete(Serializable[] entityids)
/*     */   {
/*  58 */     if ((entityids != null) && (entityids.length > 0)) {
/*  59 */       Query query = this.em.createQuery("delete from BookChapter o where o.bookid=?1");
/*  60 */       for (Serializable entityid : entityids) {
/*  61 */         query.setParameter(1, entityid);
/*  62 */         query.executeUpdate();
/*     */ 
/*  64 */         super.delete(new Serializable[] { entityid });
/*     */ 
/*  67 */         String realDirPath = ServletActionContext.getServletContext().getRealPath(CommonConstant.TXTPATH) + 
/*  68 */           CommonConstant.DELIMITER + entityid;
/*     */ 
/*  70 */         CommonUtil.deleteFile(realDirPath);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   @Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
/*     */   public boolean findBySiteid(Integer siteid)
/*     */   {
/*  79 */     Query query = this.em.createQuery("select count(o) from BookInfo o where o.siteid=?1");
/*  80 */     query.setParameter(1, siteid);
/*  81 */     long count = ((Long)query.getSingleResult()).longValue();
/*  82 */     return count > 0L;
/*     */   }
/*     */ 
/*     */   public void setInfo(List<BookInfo> bookInfos, boolean needBookType, boolean needLastChapter)
/*     */   {
/*  87 */     if ((bookInfos == null) || (bookInfos.size() == 0)) {
/*  88 */       return;
/*     */     }
/*  90 */     for (BookInfo bookInfo : bookInfos)
/*  91 */       setInfo(bookInfo, needBookType, needLastChapter);
/*     */   }
/*     */ 
/*     */   public void setInfo(BookInfo bookInfo, boolean needBookType, boolean needLastChapter)
/*     */   {
/* 103 */     if (bookInfo == null) {
/* 104 */       return;
/*     */     }
/*     */ 
/* 107 */     setReadUrl(bookInfo);
/*     */ 
/* 110 */     if (needBookType)
/*     */     {
/* 112 */       setBookType(bookInfo);
/*     */     }
/*     */ 
/* 115 */     if (needLastChapter)
/*     */     {
/* 117 */       setLastChapter(bookInfo);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setLastChapter(BookInfo bookInfo) {
/* 122 */     BookChapter chapter = getLastChapter(bookInfo.getId());
/*     */ 
/* 124 */     bookInfo.setLastChapter(chapter.getTitle());
/* 125 */     bookInfo.setLastChapterUrl(chapter.getReadurl());
/*     */   }
/*     */ 
/*     */   public void setBookType(BookInfo bookInfo)
/*     */   {
/* 130 */     BookType bookType = getBookTypeById(bookInfo.getBooktype());
/* 131 */     bookInfo.setTypename(bookType.getName());
/* 132 */     bookInfo.setTypeurl(bookType.getListurl());
/*     */   }
/*     */ 
/*     */   private void setReadUrl(BookInfo bookInfo) {
/* 136 */     PropertiesUtil properUtil = new PropertiesUtil("config.properties");
/* 137 */     if ("0".equals(properUtil.getProperty("urlwrite_tag")))
/*     */     {
/* 139 */       bookInfo.setReadurl("/home/book?bookid=" + bookInfo.getId());
/*     */     }
/*     */     else
/*     */     {
/*     */       String urlStr;
/* 143 */       if ("1".equals(properUtil.getProperty("url_mode")))
/*     */       {
/* 145 */         urlStr = "/bookinfo/" + bookInfo.getId() + ".html";
/*     */       }
/* 147 */       else urlStr = "/" + bookInfo.getId() + ".html";
/*     */ 
/* 149 */       bookInfo.setReadurl(urlStr);
/*     */     }
/*     */   }
/*     */ 
/*     */   private BookType getBookTypeById(Integer id)
/*     */   {
/* 160 */     Query query = this.em.createNativeQuery("select id, name, filename  from booktype o where o.id=?1");
/* 161 */     query.setParameter(1, id);
/*     */ 
/* 163 */     List list = query.getResultList();
/* 164 */     BookType bookType = new BookType();
/* 165 */     if ((list != null) && (list.size() > 0))
/*     */     {
/* 167 */       Object[] objs = (Object[])list.get(0);
/* 168 */       bookType.setId((Integer)objs[0]);
/* 169 */       bookType.setName((String)objs[1]);
/* 170 */       bookType.setFilename((String)objs[2]);
/*     */ 
/* 172 */       BookTypeService bookTypeService = new BookTypeServiceBean();
/* 173 */       bookTypeService.setReadurl(bookType);
/*     */     }
/*     */ 
/* 177 */     return bookType;
/*     */   }
/*     */ 
/*     */   private BookChapter getLastChapter(Integer bookid)
/*     */   {
/* 186 */     Query query = this.em.createNativeQuery("select id,title from bookchapter o where o.bookid=?1 order by id desc limit 1");
/* 187 */     query.setParameter(1, bookid);
/*     */ 
/* 189 */     BookChapter chapter = new BookChapter();
/* 190 */     List list = query.getResultList();
/* 191 */     if ((list != null) && (list.size() > 0))
/*     */     {
/* 193 */       Object[] objs = (Object[])list.get(0);
/* 194 */       chapter.setId((Integer)objs[0]);
/* 195 */       chapter.setTitle((String)objs[1]);
/* 196 */       chapter.setBookid(bookid);
/*     */ 
/* 199 */       BookChapterService chapterService = new BookChapterServiceBean();
/* 200 */       chapterService.setReadurl(chapter);
/*     */     }
/*     */ 
/* 204 */     return chapter;
/*     */   }
/*     */ 
/*     */   private List<BookChapter> getChapterIdsByBookid(Integer bookid)
/*     */   {
/* 213 */     Query query = this.em.createNativeQuery("select id,title from bookchapter o where o.bookid=?1 order by id asc");
/* 214 */     query.setParameter(1, bookid);
/*     */ 
/* 216 */     List chapters = new ArrayList();
/*     */ 
/* 218 */     List<Object[]> list = query.getResultList();
/* 219 */     if ((list != null) && (list.size() > 0)) {
/* 220 */       for (Object[] objs : list) {
/* 221 */         BookChapter chapter = new BookChapter();
/* 222 */         chapter.setId((Integer)objs[0]);
/* 223 */         chapter.setTitle((String)objs[1]);
/*     */ 
/* 225 */         chapters.add(chapter);
/*     */       }
/*     */     }
/* 228 */     return chapters;
/*     */   }
/*     */ 
/*     */   public void geneTxt(BookInfo book)
/*     */   {
/* 235 */     if (book == null) {
/* 236 */       return;
/*     */     }
/* 238 */     String realDirPath = ServletActionContext.getServletContext().getRealPath(CommonConstant.TXTPATH + "/" + book.getId());
/* 239 */     List<BookChapter> chapters = getChapterIdsByBookid(book.getId());
/*     */ 
/* 241 */     if ((chapters == null) || (chapters.size() == 0)) {
/* 242 */       return;
/*     */     }
/*     */ 
/* 245 */     String geneTxtName = realDirPath + "/" + book.getTitle();
/*     */ 
/* 247 */     CommonUtil.createDir(realDirPath);
/*     */     try
/*     */     {
/* 252 */       File file = new File(geneTxtName + ".txt");
/* 253 */       if (file.lastModified() < book.getUpdatetime().getTime())
/*     */       {
/* 255 */         FileWriter filewriter = new FileWriter(file);
/* 256 */         filewriter.write("");
/* 257 */         filewriter.close();
/*     */ 
/* 260 */         filewriter = new FileWriter(file, true);
/*     */ 
/* 264 */         for (BookChapter chapter : chapters) {
/* 265 */           String chapterPath = realDirPath + "/" + chapter.getId() + ".txt";
/* 266 */           String chapterContent = CommonUtil.readFileContent(chapterPath);
/*     */ 
/* 268 */           chapterContent = replaceHtml(chapterContent);
/*     */ 
/* 270 */           chapterContent = chapter.getTitle() + "\r\n" + chapterContent + "\r\n";
/*     */ 
/* 272 */           CommonUtil.saveStringToFileByAppend(filewriter, chapterContent);
/*     */         }
/* 274 */         filewriter.close();
/*     */ 
/* 277 */         genezip(file, geneTxtName);
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 283 */       e.printStackTrace();
/* 284 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   private void genezip(File sourcefile, String outFileName)
/*     */   {
/*     */     try {
/* 291 */       ZipOutputStream zos = new ZipOutputStream(new File(outFileName + ".zip"));
/* 292 */       FileInputStream fis = new FileInputStream(sourcefile);
/* 293 */       zos.putNextEntry(new ZipEntry(sourcefile.getName()));
/*     */ 
/* 295 */       byte[] buf = new byte[1024];
/* 296 */       int len = 0;
/* 297 */       while ((len = fis.read(buf)) != -1) {
/* 298 */         zos.write(buf, 0, len);
/*     */       }
/* 300 */       zos.closeEntry();
/* 301 */       fis.close();
/* 302 */       zos.close();
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 306 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private String replaceHtml(String srcStr)
/*     */   {
/* 312 */     if ((srcStr == null) || ("".equals(srcStr))) {
/* 313 */       return srcStr;
/*     */     }
/* 315 */     srcStr = srcStr.replaceAll("(&nbsp;)+", "");
/* 316 */     return srcStr.replaceAll("(<[a-zA-Z]*\\s*[/]*>)+", "\r\n");
/*     */   }
/*     */ }

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.service.bookinfo.BookInfoServiceBean
 * JD-Core Version:    0.6.1
 */